<template>
    <van-nav-bar title="商品评价" left-text="" left-arrow @click-left="goBack" />
    <section class="aui-flexView">
        <!-- 订单详细 -->
        <section class="aui-scrollView">
            <!-- 订单状态 -->
            <div class="aui-pay-change">
                <div class="aui-flex">
                    <div class="aui-flex-box">
                        <h3>{{ detail.status_text }}</h3>
                    </div>
                    <div class="aui-pay-zfb">
                        <img src="/assets/images/icon-zfb.png" alt="" />
                    </div>
                </div>
            </div>

            <!-- 收货地址 -->
            <div class="aui-flex">
                <div class="aui-flex-add">
                    <img :src="LoginUser.avatar" alt="" />
                </div>
                <div class="aui-flex-box">
                    <h3>
                        {{ detail.consignee }} <em> {{ detail.mobile }}</em>
                    </h3>
                    <p>{{ detail.region_text }}</p>
                </div>
            </div>

            <!-- 订单商品 -->
            <div class="aui-commodity-bg">
                <div class="item" v-for="item in detail.info">
                    <div class="aui-flex">
                        <div class="aui-commodity-img">
                            <img :src="item.product.thumb_text" />
                        </div>
                        <div class="aui-flex-box">
                            <h4 class="van-ellipsis">商品名称：{{ item.product.name }}</h4>
                            <p>商品单价：￥{{ item.price }}</p>
                            <span class="aui-barter-time">七天退换</span>
                        </div>
                        <div>
                            <h5>￥{{ item.total }}</h5>
                            <p class="aui-sml-text">X{{ item.pronum }}</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- 商品评价信息 -->
            <van-form @submit="onSubmit">
                <van-cell-group inset>
                    <!-- 评分 -->
                    <van-field name="rate" label="" :rules="[{ required: true, message: '请填写评分' }]">
                        <template #input>
                            <van-rate v-model="rate" icon="fire" void-icon="fire-o" />
                        </template>
                    </van-field>
                    <!-- 评价内容 -->
                    <van-field v-model="comment" rows="2" autosize label="" type="textarea" maxlength="120"
                        placeholder="请输入评价内容" show-word-limit />
                    <!-- 评价图片 -->
                    <van-field name="proimg" label="">
                        <template #input>
                            <van-uploader v-model="fileList" multiple :max-count="5" />
                        </template>
                    </van-field>
                </van-cell-group>
                <div style="margin: 16px;">
                    <van-button round block type="primary" native-type="submit">
                        提交
                    </van-button>
                </div>
            </van-form>

        </section>
    </section>
</template>
<script setup>
const router = useRouter();
const route = useRoute();
const goBack = () => {
    history.back()
}
const orderid = ref(route.query.orderid ?? 0)
const orderproid = ref(route.query.orderproid ?? 0)
const detail = ref({})
const { cookies } = useCookies();
const LoginUser = reactive(cookies.get("LoginUser") ?? {});
const rate = ref('')
const comment = ref('')
const fileList = ref([]);
onBeforeMount(() => {
    getInfoData()
})
const getInfoData = async () => {
    let result = await Api.orderInfo({
        busid: LoginUser.id,
        orderid: orderid.value,
        orderproid: orderproid.value
    });
    if (result.code == 1) {
        detail.value = result.data
    }
}
const onSubmit = async (value) => {
    console.log(value)
    let RequestData = new FormData()
    RequestData.append('busid', LoginUser.id)
    RequestData.append('orderid', orderid.value)
    RequestData.append('orderproid', orderproid.value)
    RequestData.append('rate', rate.value)
    RequestData.append('comment', comment.value)

    // 判断是否上传评价图片
    if (value.proimg.length > 0) {
        value.proimg.forEach((item) => {
            RequestData.append(orderproid.value + '[]', item.file)
        })
    }
    let result = await Api.orderRate(RequestData)
    if (result.code == 1) {
        showSuccessToast({
            message: result.msg,
            onClose: function () {
                goBack()
            },
        });
    } else {
        showFailToast(result.msg);
    }
}
</script>
<style>
@import url('/assets/css/info.css');
</style>